﻿using Autofac;
using AutoMapper;
using KJSyncXP.SQLite.Context;
using KJSyncXP.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KJSyncXP.Views
{
    /// <summary>
    /// UserWin.xaml 的交互逻辑
    /// </summary>
    public partial class UserWin : Window
    {
        UserManagerVM _UserManagerVM;
        List<Item> deptList = new List<Item>();
        public UserWin()
        {
            InitializeComponent();
            var scope = App._container.BeginLifetimeScope();
            var userManagerVM = scope.Resolve<UserManagerVM>();
            _UserManagerVM = userManagerVM;
            this.DataContext = _UserManagerVM;

            deptList.Add(new Item("A", "A"));
            cmbDept.ItemsSource = deptList;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            /*if (_UserManagerVM.CurrentUser.Deptname())
            {
                //GrowlUtil.ShowInfo("没选部门！", true);
                return;
            }
            if (_UserManagerVM.CurrentUser.Username.IsNullOrEmpty())
            {
                // GrowlUtil.ShowInfo("必须填写登录名！", true);
                return;
            }
            if (_UserManagerVM.CurrentUser.Userpwd.IsNullOrEmpty())
            {
                // GrowlUtil.ShowInfo("必须填写登录口令！", true);
                return;
            }*/
            DialogResult = true;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void cmbDept_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                _UserManagerVM.CurrentUser.Deptname = ((Item)e.AddedItems[0]).Value;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //var configuration = Ioc.Default.GetService<IConfiguration>();
            //var deptname = "A";
            /*if (_UserManagerVM.CurrentUser != null && _UserManagerVM.CurrentUser.Deptname?.Trim().Length == 0)
            {
                deptname = _UserManagerVM.CurrentUser.Deptname;
            }*/
            /*if (_UserManagerVM.CurrentUser != null && !_UserManagerVM.CurrentUser.Userpwd.IsNullOrEmpty())
            {
                boxPwd.Password = _UserManagerVM.CurrentUser.Userpwd;
            }

            if (deptname.IsNullOrEmpty()) return;*/

            /*foreach (var h in deptList)
            {
                if (h.Value.ToLower().Equals(deptname.ToLower()))
                {
                    cmbDept.SelectedItem = h;
                    break;
                }
            }*/
        }

        private void PasswordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            //var passpwd = CryptManager.HashPassword(boxPwd.Password);
            var passpwd = boxPwd.Password;
            _UserManagerVM.CurrentUser.Userpwd = passpwd;//boxPwd.Password;
        }


    }

    public class Item
    {
        public string Caption { get; set; }
        public string Value { get; set; }
        public Item() { }
        public Item(string caption, string value)
        {
            Caption = caption;
            Value = value;
        }
        public override string ToString()
        {
            return Caption;
        }
    }
}
